package com.qdu.dao.impl;

import com.qdu.dao.LogDao;
import com.qdu.entity.Log;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.qdu.dao.LogResponseDao;
import com.qdu.entity.LogResponse;

/**
 *
 * @author Lenovo
 */
public class LogResponseDaoImpl extends BaseDaoImpl implements LogResponseDao{

    @Override
    public int executeUpdate(String sql, Object... params) {
        return super.executeUpdate(sql, params); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<LogResponse> selectAllResponseByRemarkId(String id) {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<LogResponse> list=new ArrayList<>();
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("select * from LogCommentReply where LCid=?");
            ps.setString(1, id);
            rs=ps.executeQuery();
            while(rs.next())
            {
                list.add(new LogResponse(rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getDate(6),rs.getInt(7)));
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public int deleteResponseById(String id) {
        Connection con=null;
        PreparedStatement ps=null;
        int rows=0;
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("delete * from LogCommentReply where LCRid=?");
            ps.setString(1, id);
            rows=ps.executeUpdate();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(null, ps, con);
        }
        return rows;
    }

    @Override
    public int addResponse(LogResponse response) {
        Connection con=null;
        PreparedStatement ps=null;
        int rows=0;
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("insert into LogCommentReply values(?,?,?,?,?,?,?)");
            ps.setString(1,"1");
            ps.setString(2,response.getId());
            ps.setString(3,response.getRemarkId());
            ps.setString(4,response.getUserId());
            ps.setString(5,response.getContent());
            ps.setDate(6,response.getTime());
            ps.setInt(7,response.getAgree());
            rows=ps.executeUpdate();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(null, ps, con);
        }
        return rows;
    }

    @Override
    public int modifyResponse(String id) {
        
        Connection con=null;
        PreparedStatement ps=null;
        int rows=0;
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("update LogCommentReply set LCRagree=LCRagree+1 where LCRid=?");
            ps.setString(1, id);
            rows=ps.executeUpdate();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(null, ps, con);
        }
        return rows;
    }

    @Override
    public List<LogResponse> selectAllResponse() {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<LogResponse> list=null;
        
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("select * from LogCommentReply");
            rs=ps.executeQuery();
            while(rs.next())
            {
                list.add(new LogResponse(rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getDate(6),rs.getInt(7)));
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

   
}
